package com.dfs.parent.gateway.client.webclient;

import com.dfs.parent.common.core.domain.Result;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

import java.time.Duration;

/**
 * @Description TODO
 * @Author dahong
 * @Date 2025/8/3 16:31
 */
@Service
@RequiredArgsConstructor
public class OrderServiceClient {
    private final WebClient.Builder webClientBuilder;
    public Mono<Result> getPort() {
        return webClientBuilder.build()
                .get()
//                .uri("http://dfs-order-start/order/orderPort")
                .uri(uriBuilder -> uriBuilder
                        .scheme("http")
                        .host("dfs-order-start") // ✅ host 是服务名
                        .path("/order/orderPort")
                        .build())
                //  .header("Authorization", getCurrentToken())
                .retrieve()
                .bodyToMono(Result.class)
                .timeout(Duration.ofSeconds(3))
                    .onErrorResume(throwable -> {
                        // 可记录日志、降级返回空用户等
                        System.err.println("调用用户中心失败: " + throwable.getMessage());
                        return Mono.just(new Result().fail());
                    }); // 单独指定block超时
    }
}
